package com.campus.dao;

import com.campus.dao.provider.UserDynaSqlProvider;
import com.campus.pojo.Pager;
import com.campus.pojo.SysModule;
import com.campus.pojo.SysUser;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface UserDao {
    //根据登录名和密码，查找用户
    @SelectProvider(type = UserDynaSqlProvider.class,method = "selectByNameAndPwd")
    SysUser selectByNameAndPwd(Map<String, Object> params);

    @Select("select * " +
            "from sys_module " +
            "where moduleCode in (select moduleCode " +
                                 "from sys_role_module " +
                                 "where roleCode in (select roleCode " +
                                                    "from sys_role " +
                                                    "where roleCode in (select roleCode " +
                                                                        "from sys_user_role " +
                                                                        "where userCode = #{userCode})))")
    List<SysModule> selectModuleById(String userId);//根据用户code 查找功能模块

    @SelectProvider(type = UserDynaSqlProvider.class,method = "count")
    int count(Map<String, Object> params);

    @SelectProvider(type = UserDynaSqlProvider.class,method = "selectAreaUserByPage")
    List<SysUser> selectAreaUserByPage(Map<String,Object> params);

    @Select("select * from sys_user where delState=1 and userCode = #{userCode}")
    SysUser isExist(SysUser user);//查找指定用户

    @Select("Select count(*) from sys_user where name = #{name} and userType = #{userType}")
    int duplicateName(SysUser user);//找重名

    @Select("select count(*) from sys_user where areaId = #{areaId} and userType = 4 and delState = 1")
    int isExistAdmin(SysUser user);//是否已有管理员

    @Update("UPDATE sys_user set name = #{name} , areaId = #{areaId} , operatorId = #{operatorId} , operatorTime = now() " +
            "Where userCode = #{userCode} and delState = 1")
    int upAreaAdmin(SysUser user);//更新用户

    @Insert("INSERT INTO sys_user(userCode, name, psw, operatorId, operatorTime, delState, unitId, userType, areaId)" +
            "VALUES (#{userCode},#{name},#{psw},#{operatorId}, now(), #{delState},#{unitId},#{userType},#{areaId})")
    int addAreaAdmin(SysUser user);//新增

    @Delete("UPDATE sys_user set delState = 2,operatorId = #{operatorId}, operatorTime = now() " +
            "where userCode in (${userCode})")
    int deleteAreaUser(SysUser user);//删除

    @Update("UPDATE sys_user set psw = #{psw},operatorId = #{operatorId}, operatorTime = now()" +
            "where userCode = #{userCode}")
    int resetPassword(SysUser user);//重置密码 更新

    @Select("select * from sys_user where userCode = #{code}")
    SysUser selectByUserCode(String code);//根据主键查询用户

    @SelectProvider(type = UserDynaSqlProvider.class,method = "count1")
    List<SysUser> count1(Map<String, Object> params);

    // 根据条件，分页动态查询用户
    @SelectProvider(type = UserDynaSqlProvider.class, method = "selectWithParam1")
    List<SysUser> selectByPage1(Map<String, Object> params);

    // 检验是否存在该用户名
    @Select("select count(userCode)  " +
            "from sys_user " +
            "where name = #{name} and userCode <> #{userCode} and areaId=#{areaId} and delState = 1")
    int isExistName(@Param("name") String name, @Param("userCode") String userCode, @Param("areaId") String areaId);

    // 检验该单位是否已经被绑定
    @Select("select count(userCode) " +
            "from sys_user " +
            "where unitId = #{unitId} and userCode <> #{userCode} and unitId <> '' and areaId =#{areaId} and unitId is not null and delState = 1")
    int isExistUnit(@Param("unitId") String unitId, @Param("userCode") String userCode, @Param("areaId") String areaId);

    // 修改用户
    @Update("update sys_user set name=#{name},unitId=#{unitId},userType=#{userType},operatorId=#{operatorId},operatorTime=now() where userCode=#{userCode} and delState=1")
    int upUser(SysUser user);

    // 从sys_user_role表中根据userCode获取roleCode列表
    @Select("select roleCode from sys_user_role where userCode=#{userCode}")
    List<String> getCheckedRole(@Param("userCode") String userCode);

    // 给用户绑定新的角色
    @Insert("insert into sys_user_role(urId,userCode,roleCode,areaId) " +
            "values (#{urId},#{userCode},#{roleCode},#{areaId})")
    int bindRoleByUserId(@Param("urId") String urId, @Param("userCode") String userCode, @Param("roleCode") String roleCode, @Param("areaId") String areaId);

    // 删除指定用户编号的角色
    @Delete("delete from sys_user_role where userCode=#{userCode}")
    public int delRoleByUserId(@Param("userCode") String userCode);
}

